function vardata = rslice_retrieve_data ( gdata )
% RSLICE_RETRIEVE_DATA:  retrieve the necessary data
%
% The retrieved hyperslab will be at most a 3D variable.  
%
% PARAMETERS:
% Input:
%     gdata:
%         Structure.  Fields used are
%             ncfile:  Path to NetCDF file having the requested variable
%             varname:  name of NetCDF variable we wish to retrieve
%             varinfo:  structure of metadata for the variable
%             timestep:  If the variable is a time variable, use this to
%                        figure out when to get the data.
%
% Output:
%     vardata:
%         If there is a singleton dimension after the data has been retrieved,
%         we squeeze it out.  This array should be at least 2D, but no more than
%         3D.  There should be no time dependence after this.
%
% Calls:
%     rslice_message
% Called By:
%     rslice_xslice
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% $Name: rslice-0_9_2 $
% $Id: rslice_retrieve_data.m,v 1.2 2005/06/29 21:21:58 jevans Exp $
% AUTHOR:  johnevans@acm.org
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


if gdata.verbose > 0
	rslice_message ( sprintf ( '%s:  variable is %s', mfilename, gdata.varname ) );
end

%
% Ok, need to figure out the start index for the current file.
local_timestep = gdata.timestep - gdata.ncfile_start_index(gdata.ncfile_index);



%
% Depending upon the size of the data, set the start and count
% arguments accordingly.
switch ( gdata.dim_type )
case 'TZYX'
	start = [local_timestep 0 0 0];
	count = [1 -1 -1 -1];
case 'TYX'
	start = [local_timestep 0 0];
	count = [1 -1 -1];
case 'ZYX'
	start = [0 0 0];
	count = [-1 -1 -1];
case 'YX'
	start = [0 0];
	count = [-1 -1];
otherwise
	msg = sprintf ( '%s:  unhandled vartype %s, variable %s.\n', mfilename, gdata.vartype, gdata.varname );
	rslice_error ( msg );

end


vardata = nc_varget ( gdata.ncfile_list{gdata.ncfile_index}, gdata.varname, start, count );

vardata = squeeze ( vardata );

return
